package org.jeecg.modules.zhima.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.zhima.admin.entity.ZmPlayer;
import org.jeecg.modules.zhima.admin.service.IZmPlayerService;
import org.jeecg.modules.zhima.portal.domain.Player;
import org.jeecg.modules.zhima.portal.service.IPlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
* @Description: zm_player
* @Author: jeecg-boot
* @Date:   2021-02-22
* @Version: V1.0
*/
@Api(tags="选手")
@RestController
@RequestMapping("/player")
@Slf4j
public class MZmPlayerController extends JeecgController<ZmPlayer, IZmPlayerService> {
   @Autowired
   private IZmPlayerService zmPlayerService;
   @Autowired
   private IPlayerService playerService;

   /**
    * 分页列表查询
    *
    * @param zmPlayer
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "zm_player-分页列表查询")
   @ApiOperation(value="zm_player-分页列表查询", notes="zm_player-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(ZmPlayer zmPlayer,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<ZmPlayer> queryWrapper = QueryGenerator.initQueryWrapper(zmPlayer, req.getParameterMap());
       Page<ZmPlayer> page = new Page<ZmPlayer>(pageNo, pageSize);
       IPage<ZmPlayer> pageList = zmPlayerService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param zmPlayer
    * @return
    */
   @AutoLog(value = "zm_player-添加")
   @ApiOperation(value="zm_player-添加", notes="zm_player-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody ZmPlayer zmPlayer) {
       zmPlayerService.save(zmPlayer);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param zmPlayer
    * @return
    */
   @AutoLog(value = "zm_player-编辑")
   @ApiOperation(value="zm_player-编辑", notes="zm_player-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody ZmPlayer zmPlayer) {
       zmPlayerService.updateById(zmPlayer);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "zm_player-通过id删除")
   @ApiOperation(value="zm_player-通过id删除", notes="zm_player-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       zmPlayerService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "zm_player-通过id查询")
   @ApiOperation(value="zm_player-通过id查询", notes="zm_player-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       ZmPlayer zmPlayer = zmPlayerService.getById(id);
       if(zmPlayer==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(zmPlayer);
   }

    /**
     * 通过userId查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "zm_player-通过userId查询")
    @ApiOperation(value="zm_player-通过userId查询", notes="zm_player-通过userId查询")
    @GetMapping(value = "/getByUserId")
    public Result<?> getByUserId(@RequestParam(name="id",required=true) String id) {
        Player player = playerService.getByUserId(id);
        if(player==null) {
            return Result.error("未找到对应数据");
        }
        if (ObjectUtils.isNotEmpty(player.getIntegral()) && ObjectUtils.isNotEmpty(player.getIntegralAward())){
            player.setIntegral(player.getIntegral()+player.getIntegralAward());
        }
        return Result.OK(player);
    }

}
